//
//  NewFeatureController.m
//  Recycling
//
//  Created by jzd on 2018/5/16.
//  Copyright © 2018年 宋佳. All rights reserved.
//

#import "NewFeatureController.h"
#import "NewFeatureCell.h"
#import "LoginViewController.h"

@interface NewFeatureController ()

@end

@implementation NewFeatureController

static NSString * const reuseIdentifier = @"featureCell";


- (instancetype)init
{
    // 流水布局
    UICollectionViewFlowLayout *layout = [[UICollectionViewFlowLayout alloc] init];
    
    // 设置cell的尺寸
    layout.itemSize = [UIScreen mainScreen].bounds.size;
    
    // 设置每一行的间距
    layout.minimumLineSpacing = 0;
    
    // 设置每个cell的间距
    layout.minimumInteritemSpacing = 0;
    
    // 设置滚动方向
    layout.scrollDirection = UICollectionViewScrollDirectionHorizontal;
  
    return [self initWithCollectionViewLayout:layout];
}

- (void)viewDidLoad {
    [super viewDidLoad];
    
    [self setUpCollectionView];
    
}

// 初始化CollectionView
- (void)setUpCollectionView
{
    // 注册cell
    [self.collectionView registerNib:[UINib nibWithNibName:NSStringFromClass([NewFeatureCell class]) bundle:nil] forCellWithReuseIdentifier:reuseIdentifier];
    // 取消弹簧效果
    self.collectionView.bounces = NO;
    
    // 取消显示指示器
    self.collectionView.showsHorizontalScrollIndicator = NO;
    
    // 开启分页模式
    self.collectionView.pagingEnabled = YES;
    
}
-(NSInteger)numberOfSectionsInCollectionView:(UICollectionView *)collectionView
{
    return 1;
}
#pragma mark <UICollectionViewDataSource>
- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section {
    return 2;
}
- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath {
    
    NewFeatureCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:reuseIdentifier forIndexPath:indexPath];
    int page = (int)(indexPath.row +1);
    NSString *imageName = [NSString stringWithFormat:@"p%d.jpg",page];
    NSString *imagePath = [[NSBundle mainBundle] pathForResource:[NSString stringWithFormat:@"%@",imageName] ofType:nil];
    cell.image = [UIImage imageWithContentsOfFile:imagePath];
    cell.currentPage.currentPage = indexPath.row;
    if (indexPath.row == 0) {
        cell.selectButton.hidden = YES;
    }else{
        cell.selectButton.hidden = NO;
    }
    return cell;
}

#pragma mark <UICollectionViewDelegate>
- (void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath
{
    if (indexPath.row == 1) {
        // 获取最新的版本号
         NSDictionary *dict =  [NSBundle mainBundle].infoDictionary;
        NSString *curVersion = dict[@"CFBundleShortVersionString"];
        [[NSUserDefaults standardUserDefaults] setObject:curVersion forKey:VersionKey];
        // 切换窗口的根控制器进行跳转
        [UIApplication sharedApplication].keyWindow.rootViewController = [[LoginViewController alloc]init];
    }
    
}
@end
